package com.yanqu.road.server.manger.activity.newyear2023;


import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.player.PlayerState;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.BulletScreenProto;
import com.yanqu.road.pb.activity.FireWorkProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.bulletscreen.BulletScreenModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.task.GameCommandTask;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.executor.GeneralThreadFactory;
import com.yanqu.road.utils.http.HttpHelper;

import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FireWorkDeBugMgr {

    private static RandomHelper randomHelper = new RandomHelper();

    private static int openTableNum = 1;
    private static int joinRandomNum = 2;

    public static void testTask(){
        for(int i = 0; i < openTableNum ; i++){
            openOneTable();
        }
        for(int i = 0; i < joinRandomNum ; i++){
            joinRandomTable();
        }
    }

    private static void openOneTable(){
        List<UserInfo> userInfoList = UserMgr.getUserInfoList();
        int index = randomHelper.next(0,userInfoList.size());
        UserInfo userInfo = userInfoList.get(index);
        /*GamePlayer player = GamePlayerMgr.getPlayer(userInfo.getUserId());
        if(player != null){

        }*/
        FireWorkProto.CrossFireWorkOpenRedPacketTableReqMsg.Builder msg = FireWorkProto.CrossFireWorkOpenRedPacketTableReqMsg.newBuilder();
        msg.setActivityId(FireWorkMgr.getRedPacketActivityId());
        msg.setIntegral(Long.MAX_VALUE);
        msg.setMessage("测试开桌子");
        msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId())));
        YanQuMessage pb2Msg = YanQuMessageUtils.buildMessage(Protocol.C2_FIREWORK_ACTIVITY_OPEN, msg);
        GamePlayerMgr.sendPacket(userInfo.getUserId(), pb2Msg);
    }

    private static void joinRandomTable(){
        List<UserInfo> userInfoList = UserMgr.getUserInfoList();
        int index = randomHelper.next(0,userInfoList.size());
        UserInfo userInfo = userInfoList.get(index);
        GamePlayer player = GamePlayerMgr.getPlayer(userInfo.getUserId());
        if(player != null){
            FireWorkProto.CrossFireWorkRobRedPacketRandomReqMsg.Builder msg = FireWorkProto.CrossFireWorkRobRedPacketRandomReqMsg.newBuilder();
            msg.setActivityId(FireWorkMgr.getRedPacketActivityId());
            msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId())));
            YanQuMessage pb2Msg = YanQuMessageUtils.buildMessage(Protocol.C2_FIREWORK_ACTIVITY_ROB_RANDOM, msg);
            GamePlayerMgr.sendPacket(player.getUserId(), pb2Msg);

            //直接加次数吧，不然弹幕发不出
            player.getModule(BulletScreenModule.class).robAddCanBarrageTimes(FireWorkMgr.getRedPacketActivityId());
            player.getModule(BulletScreenModule.class).initFireWorkUserData(FireWorkMgr.getRedPacketActivityId());
            YanQuMessage message = parseMessageMsg(player.getUserId());
            GameCommandTask cmdWrapper = new GameCommandTask(null, message, player);
            player.setState(PlayerState.ONLINE);
            player.getCommandQueue().add(cmdWrapper);
        }
    }


    private static YanQuMessage parseMessageMsg(long userId) {
        BulletScreenProto.BulletScreenContentReqMsg.Builder reqMsg = BulletScreenProto.BulletScreenContentReqMsg.newBuilder();
        reqMsg.setContent(userId + "的谢谢红包弹幕");
        reqMsg.setActivityId(FireWorkMgr.getRedPacketActivityId());
        return YanQuMessageUtils.buildMessageByte(Protocol.S_ACTIVITY_BULLET_SCREEN_COMMENTS, userId, reqMsg.build().toByteArray());
    }

    public static boolean isDebug() {
        return false;
    }

    public static void main(String[] args) {
        //373
        /*for(int i = 74; i <= 2310; i++){
            int serverId = 30100000 + i;
            //https://dzg301-proxy.ngty556.com/30100001_ws/Web
            String url = MessageFormat.format("https://dzg301-proxy.ngty556.com/{0}_ws/Web/execCommand", "" + serverId);
            System.out.println(url);
            ExecutorService threadPool = Executors.newFixedThreadPool(1, new GeneralThreadFactory("httpThread"));
            threadPool.execute(() -> {
                JSONObject paramObj = new JSONObject();
                paramObj.put("userId", 0);
                paramObj.put("commands", "repair,repairRemoveGoodabc");
                String result = HttpHelper.doPostJson(url, paramObj.toJSONString(), "UTF-8");
                System.out.println(result);
            });
        }*/
        String url = MessageFormat.format("https://dzg311-proxy.ngty556.com/{0}_ws/Web/execCommand","31100092");
        System.out.println(url);
        ExecutorService threadPool = Executors.newFixedThreadPool(1, new GeneralThreadFactory("httpThread"));
        threadPool.execute(() -> {
            JSONObject paramObj = new JSONObject();
            paramObj.put("userId", 0);
            paramObj.put("commands", "repair,repairRemoveGoodabc");
            String result = HttpHelper.doPostJson(url, paramObj.toJSONString(), "UTF-8");
            System.out.println(result);
        });
    }
}
